//
//  NetworkManager.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/5/23.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "NetworkManager.h"
#import <AFNetworkReachabilityManager.h>
#import "IPToolManager.h"

@interface NetworkManager()


@end


@implementation NetworkManager



+ (instancetype)shareInstance{
    static dispatch_once_t onceToken;
    static NetworkManager *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[NetworkManager alloc] init];
    });
    return instance;
}



- (void)enableNetwork{
    [self checkNetReachability];
}

- (NSString *)currentDeviceIP{
    if (IsEmptyStr(_currentDeviceIP)) {
        return @"0.0.0.0";
    }
    return _currentDeviceIP;
}

- (void)checkNetReachability{
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
    //监听改变
    [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
          IPToolManager *ipManager = [IPToolManager sharedManager];
          switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                self.networkReachabilityStatus = CedarNetworkReachabilityStatusNone;
                 CLog(@"Unknow Net");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                self.networkReachabilityStatus = CedarNetworkReachabilityNoneNet;
                  CLog(@"No Net");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                self.networkReachabilityStatus = CedarNetworkReachabilityStatusWWAN;
                CLog(@"3G|4G Net");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                self.networkReachabilityStatus = CedarNetworkReachabilityStatusWIFI;
                CLog(@"WiFi Net");
                break;
            default:
                self.networkReachabilityStatus = CedarNetworkReachabilityStatusNone;
                break;
        }
        if (self.networkReachabilityStatus == CedarNetworkReachabilityStatusWWAN || self.networkReachabilityStatus ==   CedarNetworkReachabilityStatusWIFI) {
            BOOL wifi =  self.networkReachabilityStatus == CedarNetworkReachabilityStatusWIFI?YES:NO;
            _currentDeviceIP = [ipManager currentIPV4Adress:wifi];
        }
    }];
    [manger startMonitoring];
}


@end
